<?php
    require_once(dirname(__FILE__).'/global.inc');
    $pageindex = 1;
    $nextbutton_action = 'checkcode();';
    $prevpage = null;
    $nextpage = '/infomat/select_spec.php';
?>
        <!-- Сканер Штрих Кода -->
        <div style="position: absolute; left: 0; top:0;">
            <object ID="infomatCom" CLASSID="CLSID:5E53C4E3-6417-41E6-9B62-497FBA1B84BB" width="100" height="100"
            CODEBASE = "infomat.dll">
            </object>
        </div>
        <form id ="frm" style="display: none" method="POST" action="select_post.php">
            <input name="code" id="ccc">
            <input type="submit">
        </form>
        <script language="VBScript">
            Dim ccc2, cccform2
            Set ccc2 = document.getElementById("ccc")
            Set cccform2 = document.getElementById("frm")
            Call infomatCom.OpenComPort(2,"9600,N,8,1",,,,vbCr)
            Sub infomatCom_Receive(buf)
                xalert2("ПОДОЖДИТЕ...")
                infomatCom.CloseComPort
                ccc2.value = buf
                cccform2.submit
            End Sub
        </script>
<?php
    if(!is_null($error_text = $mikron->Cache->getCache('error_text')))
    {
        ?>
        <script type="text/javascript">
            xalert('<?php echo $error_text; ?>');
        </script>
        <?php
        $mikron->Cache->setCache('error_text', null);
    }
?>

<table border="0" width="100%" height="100%">
    <tr>
        <td colspan="2" style="font-size: 2em;">&nbsp;</td>
    </tr>
    <tr>
        <td align="center">
            <table width="10" border="0">
                <tr>
                    <td class="big_text_green">Номер Вашей амбулаторной карты:</td>
                </tr>
                <tr>
                    <td style="white-space: nowrap;">
                        <span id="tbox1" class="inp_tbox">&nbsp;&nbsp;&nbsp;&nbsp;</span> <font class="big_text">&ndash;</font>
                        <span id="tbox2" class="inp_tbox">&nbsp;&nbsp;&nbsp;&nbsp;</span> <font class="big_text">&ndash;</font>
                        <span id="tbox3" class="inp_tbox">&nbsp;&nbsp;&nbsp;&nbsp;</span> <font class="big_text">&ndash;</font>
                        <span id="tbox4" class="inp_tbox">&nbsp;&nbsp;&nbsp;&nbsp;</span> <font class="big_text"></font>
                        <br>&nbsp;<?php require_once(dirname(__FILE__).'/help.inc');?>
                    </td>
                </tr>
                <tr>
                    <td class="big_text_green">Для пользования системой получите штрихкод в регистратуре.</td>
                </tr>
            </table>
        </td>
        <td align="left" valign="top" style="padding-left: 1em;">

            <form action="<?php echo $_SERVER['REQUEST_URI']; ?>" method="post">
                <table border="0">
                    <tr>
                    <td>
                        <table border="0" cellspacing="0" cellpadding="10">
                        <tr>
                            <td class="digitbuttonclass"><?php addbutton('1','',1);?></td>
                            <td class="digitbuttonclass"><?php addbutton('2','',2);?></td>
                            <td class="digitbuttonclass"><?php addbutton('3','',3);?></td>
                        </tr>
                        <tr>
                            <td class="digitbuttonclass"><?php addbutton('4','',4);?></td>
                            <td class="digitbuttonclass"><?php addbutton('5','',5);?></td>
                            <td class="digitbuttonclass"><?php addbutton('6','',6);?></td>
                        </tr>
                        <tr>
                            <td class="digitbuttonclass"><?php addbutton('7','',7);?></td>
                            <td class="digitbuttonclass"><?php addbutton('8','',8);?></td>
                            <td class="digitbuttonclass"><?php addbutton('9','',9);?></td>
                        </tr>
                        <tr>
                            <td><?php addbutton('_cr', 'javascript: backspace();', 10);?></td>
                            <td><?php addbutton('0','',0);?></td>
                            <td>&nbsp;</td>
                        </tr>
                        </table>
                        </td>
                    </tr>
                </table>
            </form>

            <script type="text/javascript">

                var codelength = <?php echo $codelength;?>;
                var scancode = "";
                var buffer = "<?php
                                    if(isset($_GET['code']))
                                    {
                                        echo $_GET['code'];
                                    }
                                    elseif(isset($_GET['recode']))
                                    {
                                        echo $_GET['recode'];
                                    }
                                    ?>";
                if(buffer.substr(0,1) == "L"){buffer = "";}
                drawbuffer();
                function pressed(e)
                {
                    if(event.keyCode == 13)
                    { 
                        if(scancode.substr(0, 1) == "L")
                        {
                            var ccc = document.getElementById("ccc");
                            var cccform = document.getElementById("frm");
                            adr = scancode;
                            scancode = "";
                            ccc.value = adr;
                            cccform.submit();
                        }
                        else
                        {
                            scancode = "";
                            if(buffer.length == codelength)
                            {
                                checkcode();      
                            }
                            else
                            {
                                xalert("Ошибка сканирования штрих-кода.");
                            }
                        }
                    }
                    else
                    {
                        var char = String.fromCharCode(event.keyCode);
                        if(scancode.length > 0)
                        {
                            if(scancode.substr(0, 1) != "L")
                            {
                                scancode = "";
                            }
                        }
                        scancode += char;
                        if(scancode.substr(0, 1) != "L")
                        {
                            if((char.charCodeAt(0) >= 48) && (char.charCodeAt(0) <= 57))
                            {
                                if(buffer.length < codelength)
                                {
                                    buffer += char;
                                    drawbuffer();
                                }
                            }
                        }
                    }
                }

                document.body.onkeypress = pressed;

                function correct_len(text, len)
                {
                    var i;
                    var tmp = '';
                    for(i=0;i<len;i++)
                    {
                      tmp += ' ';
                    }
                    tmp = text + tmp;
                    tmp = tmp.substring(0, len);
                    return tmp;
                    //var tmp = text + string.space(len);
                }

                function addchar(char_code)
                {
                    if(buffer.length < codelength)
                    {
                        buffer += char_code;
                        drawbuffer();
                    }  
                    if(buffer.length == codelength)
                    {
                        //checkcode();
                    }      
                }

                function checkcode()
                {
                    if(buffer == '422100')
                    {
                        xalert("Автор программы для информационного терминала — Ленар Закиров.<br />Адрес для связи: sciner@yandex.ru<br>Сделано в Казани 21 августа 2008г.");
                    }
                    else
                    {
                        var checked = true;
                        if(buffer.length != codelength)
                        {
                            checked = false;
                        }
                        if(checked)
                        {
                            okcode();
                        }
                        else
                        {
                            badcode();
                        }        
                    }
                }
                
                function okcode()
                {
                    xalert2('ПОДОЖДИТЕ...');
                    try
                    {
                        infomatCom.CloseComPort();
                    }
                    catch(err){}
                    location.href = '<?php echo $nextpage; ?>?code=' + buffer;
                }

                function badcode()
                {
                    xalert("Введен неверный код пациента!");
                }

                function drawbuffer()
                {

                    buffer2 = correct_len(buffer, codelength);
                    
                    var text1 = buffer2.substr(0, 4);
                    var text2 = buffer2.substr(4, 4);
                    var text3 = buffer2.substr(8, 4);
                    var text4 = buffer2.substr(12, 4);

                    var vtbox1 = document.getElementById("tbox1");
                    var vtbox2 = document.getElementById("tbox2");
                    var vtbox3 = document.getElementById("tbox3");
                    var vtbox4 = document.getElementById("tbox4");

                    vtbox1.innerHTML = correct_len(text1, 4).replace(/ /g, '&nbsp;');
                    vtbox2.innerHTML = correct_len(text2, 4).replace(/ /g, '&nbsp;');
                    vtbox3.innerHTML = correct_len(text3, 4).replace(/ /g, '&nbsp;');
                    vtbox4.innerHTML = correct_len(text4, 4).replace(/ /g, '&nbsp;');

                }
                function backspace()
                {
                    if(buffer.length>0)
                    {
                        buffer = buffer.substr(0, buffer.length-1);
                        drawbuffer();
                    }
                }

                function m_down(obj, char, index)
                {   
                    var x = -index*<?php echo $keupad_button_width;?>;
                    obj.style.backgroundPosition = x+" -<?php echo $keupad_button_width;?>";
                }

                function m_up(obj, char, index)
                {
                    var x = -index*<?php echo $keupad_button_width;?>;
                    obj.style.backgroundPosition = x+" top";
                }
            </script>
        </td>
    </tr>
</table>

<?php $mikron->Page->EndBody(); ?>